/*
Copyright (c) 2010 k.schulz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package de.altitudecustomcommands;


import de.altitude.cimuelle.enums.Perk;
import de.altitude.cimuelle.enums.Plane;
import de.altitude.cimuelle.enums.Skin;
import de.altitude.cimuelle.player.PlayerConfiguration;
import de.altitude.cimuelle.player.playerSessionStats;

import java.io.Serializable;

/**
 * This class is a representation of a player. It holds all important data of
 * this player and provides get and set methods if needed.
 *
 * @author k.schulz
 */
public class Player implements Serializable {

    private static final long serialVersionUID = 1000100004L;
    public static final String BOTVAPOR = "00000000-0000-0000-0000-000000000000";

    private final PlayerConfiguration playerConfiguration = new PlayerConfiguration();

    /**
     * Construct a new Player Object with all data needed.
     *
     * @param pName        The Player nickname
     * @param pVapor       The Player VaporID
     * @param pIP          The PlayerIP
     * @param port         The VirtualServerPort
     * @param playerNumber The players Number
     */

    public Player(String pName, String pVapor, String pIP, String port, int playerNumber) {
        this.name = pName;
        this.vaporID = pVapor;
        String[] tmp = pIP.split(":");
        this.IP = tmp[0];
        this.PORT = port;
        this.playerNumber = playerNumber;
        this.team = 2;
        this.isAlive = false;
    }

    private int team;

    /**
     * Gets the player's current teamID.
     *
     * @return Returns the integer value representing the player's team
     */

    public int getTeam() {
        return this.team;
    }

    /**
     * Sets the player's current teamID.
     *
     * @param setTeam The value of the player's current team.
     */

    public void setTeam(int setTeam) {
        this.team = setTeam;
    }


    private final int playerNumber;

    /**
     * Gets the player's playerID.
     *
     * @return The player's playerID on his virtual Server.
     */

    public int getPlayerNumber() {
        return this.playerNumber;
    }

    private String IP = null;

    /**
     * Gets a String representation of the players IP address.
     *
     * @return The player's IPv4:port as String value.
     */

    public String getIP() {
        return this.IP;
    }


    private String PORT = null;

    /**
     * Gets the serverport, the player is playing on.
     *
     * @return The Listenport of the virtual server this player is playing on as a string.
     */

    public String getPort() {
        return this.PORT;
    }

    private String name = null;

    /**
     * Gets the player's nickname.
     *
     * @return The player's nickname.
     */

    public String getName() {
        return this.name;
    }

    /**
     * Sets a new nickname for a player. This is needed if a player changes his
     * nickname while playing on your server.
     *
     * @param name The player's new nickname.
     */

    public void setName(String name) {
        this.name = name;
    }


    private String vaporID = null;

    /**
     * Gets the player's vaporID.
     *
     * @return The player's vaporID as a string.
     */

    public String getVapor() {
        return this.vaporID;
    }

    public void setAlive(boolean alive) {
        isAlive = alive;
    }

    private boolean isAlive = false;

    public boolean isAlive() {
        return isAlive;
    }

    /**
     * Returns if the specific playerNumber for a port is the playerNumber of this Player
     *
     * @return
     */

    public boolean isPlayerIDonPort(int id, int port) {
        return (this.playerNumber == id && this.PORT.equals(Integer.toString(port)));
    }

    /**
     * This method can determinate if this player object represents a bot.
     *
     * @return Returns true, if this player object is a bot
     */
    public boolean playerIsBot() {
        return (this.name.startsWith("Bot")
                && this.vaporID.equals(BOTVAPOR));
    }

    private final playerSessionStats stats = new playerSessionStats();

    public playerSessionStats getStats() {
        return stats;
    }

    public void setSpawnData(Plane plane, Perk perkRed, Perk perkGreen, Perk perkBlue, Skin skin, int team) {
        this.playerConfiguration.perkGreen = perkGreen;
        this.playerConfiguration.perkRed = perkRed;
        this.playerConfiguration.perkBlue = perkBlue;
        this.playerConfiguration.skin = skin;
        this.playerConfiguration.plane = plane;

        this.team = team;
        this.isAlive = true;
    }

    @Override
    public String toString() {
        return "Player{" +
                "team=" + team +
                ", playerNumber=" + playerNumber +
                ", IP='" + IP + '\'' +
                ", PORT='" + PORT + '\'' +
                ", name='" + name + '\'' +
                ", vaporID='" + vaporID + '\'' +
                ", stats=" + stats +
                '}';
    }

    public PlayerConfiguration getPlayerConfiguration() {
        return playerConfiguration;
    }
}
